import {useHttpHook} from './index'

export default function useAreaHook() {
  const [ area, areaLoading ] = useHttpHook({url: '/area', method: 'get'})

  return [ formatData(area), areaLoading ]
}

function formatData(area) {
  if (area instanceof Array) {
    const temp = JSON.parse(JSON.stringify(area))
    for (let i = 0, len = area.length; i < len; i++) {
      temp[i].value = temp[i].id
      temp[i].label = temp[i].name
      if (temp[i].children && temp[i].children.length) {
        temp[i].children = formatData(temp[i].children)
      }
    }
    return temp
  } else {
    return []
  }

}
